package filehandling;

import java.io.File;

import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;

import parser.AdventureParse;
import adventure.Adventure;

/**
 *
 * @author Michael Hanns
 *
 */
public class Opening {

    public static String openPrompt() {
        JFileChooser fc = new JFileChooser();

        FileFilter filter = new FileFilter() {

            @Override
            public boolean accept(File f) {
                return f.isDirectory() || f.getName().toLowerCase().endsWith(".xml");
            }

            @Override
            public String getDescription() {
                return "*.xml - eXtensible Markup Language file";
            }
        };

        fc.setFileFilter(filter);
        fc.setCurrentDirectory(new File(System.getProperty("user.dir") + "/modules/"));

        int returnVal = fc.showOpenDialog(null);

        String fileName = "";

        if (returnVal == JFileChooser.APPROVE_OPTION) {
            fileName = fc.getSelectedFile().getAbsolutePath();
            return fileName;
        }
        return null;
    }

    public static Adventure readFromFile(String filename) {
        File file = new File(filename);
        AdventureParse p = new AdventureParse();
        p.readAdventureFile(file);
        return p.getAdventure();
    }
}
